home *** CD-ROM | disk | FTP | other *** search
- VERSION 2.00
- Begin Form Form1
- Caption = "Shell Sort Example"
- ClientHeight = 4470
- ClientLeft = 1095
- ClientTop = 1485
- ClientWidth = 7365
- Height = 4875
- Left = 1035
- LinkTopic = "Form1"
- ScaleHeight = 4470
- ScaleWidth = 7365
- Top = 1140
- Width = 7485
- Begin ListBox List3
- Height = 2955
- Left = 4080
- Sorted = -1 'True
- TabIndex = 2
- Top = 465
- Width = 1755
- End
- Begin ListBox List2
- Height = 2955
- Left = 2175
- TabIndex = 1
- Top = 465
- Width = 1755
- End
- Begin ListBox List1
- Height = 2955
- Left = 180
- TabIndex = 0
- Top = 480
- Width = 1755
- End
- Begin Label Label1
- Caption = "Sorted ListBox"
- Height = 255
- Index = 2
- Left = 4080
- TabIndex = 5
- Top = 195
- Width = 1755
- End
- Begin Label Label1
- Caption = "Sorted List"
- Height = 255
- Index = 1
- Left = 2160
- TabIndex = 4
- Top = 180
- Width = 1755
- End
- Begin Label Label1
- Caption = "Unsorted List"
- Height = 255
- Index = 0
- Left = 195
- TabIndex = 3
- Top = 180
- Width = 1755
- End
- ' Shell Sort routine for Visual Basic
- ' Harry JF Wykes, End User Computing Limited, 100014.2573@compuserve.com
- ' Sorts an array of strings into ascending order.
- ' Can be easily modified to deal with arrays of other types
- ' including user defined types.
- ' The sort can be made ascending by modifying the string
- ' comparison operator.
- ' Ported from C when I needed to sort an array of 12000 strings
- ' and a Sorted Listbox bombed out after 2000-odd records were added
- ' to it.
- ' I made a beeline for the VB forum, searched the libraries for 'SORT'
- ' and was astonished to find not a thing. So if anyone else needs a
- ' VB sort routine, here it is.
- ' The sort is a Shell Sort. For more details see Robert Sedgewick's
- ' excellent book 'Algorythms'
- Sub Form_Load ()
- Dim S() As String
- ReDim S(12) As String
- S(1) = "One"
- S(2) = "Two"
- S(3) = "Three"
- S(4) = "Four"
- S(5) = "Five"
- S(6) = "Six"
- S(7) = "Seven"
- S(8) = "Eight"
- S(9) = "Nine"
- S(10) = "Ten"
- S(11) = "Eleven"
- S(12) = "Twelve"
- T% = 12
- For Ctr% = 1 To T%
- List1.AddItem S(Ctr%)
- Next Ctr%
- ShellSort S()
- For Ctr% = 1 To T%
- List2.AddItem S(Ctr%)
- Next Ctr%
- For Ctr% = 1 To T%
- List3.AddItem S(Ctr%)
- Next Ctr%
- End Sub
- Sub ShellSort (S() As String)
- ' Sort the array S
- Dim Gap As Integer
- Dim i As Integer
- Dim j As Integer
- Dim n As Integer
- n = UBound(S)
- Gap = n / 2
- Do While Gap > 0
- i = Gap
- For i = Gap To n
- j = i - Gap
- Do While j > 0
- If S(j) < S(j + Gap) Then
- Exit Do
- End If
- T$ = S(j)
- S(j) = S(j + Gap)
- S(j + Gap) = T$
- j = j - Gap
- Loop
- Next i
- Gap = Gap / 2
- End Sub
-